package com.kevin.ssh2jquery.aop;

import org.apache.commons.logging.Log;
import org.springframework.aop.interceptor.CustomizableTraceInterceptor;

public class MethodTraceInterceptor extends CustomizableTraceInterceptor {

	private static final long serialVersionUID = 1L;

	private static enum LogLevel {
		TRACE, DEBUG, INFO
	};

	private LogLevel logLevel = LogLevel.TRACE;

	public void setLogLevel(String logLevel) {
		this.logLevel = LogLevel.valueOf(logLevel);
	}

	@Override
	protected void writeToLog(Log logger, String message, Throwable ex) {
		if (ex != null) {
			switch (this.logLevel) {
			case TRACE:
				logger.trace(message, ex);
				return;
			case DEBUG:
				logger.debug(message, ex);
				return;
			case INFO:
				logger.info(message, ex);
				return;
			default:
				return;
			}
		} else {
			switch (this.logLevel) {
			case TRACE:
				logger.trace(message);
				return;
			case DEBUG:
				logger.debug(message);
				return;
			case INFO:
				logger.info(message);
				return;
			default:
				return;
			}
		}
	}

	@Override
	protected boolean isLogEnabled(Log logger) {
		switch (this.logLevel) {
		case TRACE:
			return logger.isTraceEnabled();
		case DEBUG:
			return logger.isDebugEnabled();
		case INFO:
			return logger.isInfoEnabled();
		default:
			return false;
		}
	}
}
